package com.vf.admin.utils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * ResPonse返回内容处理工具类
 */
public class ResPonseRetUtil {
    private ResPonseRetUtil(){
    }
    public static ResPonseRetUtil getInstance(){
        return new ResPonseRetUtil();
    }
    /**
     * 发送消息 text/html;charset=utf-8
     * @param response
     * @param str
     * @throws Exception
     */
    public void sendMessage(HttpServletResponse response, String str) throws Exception {
        response.setContentType("text/html; charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.print(str);
        writer.close();
        response.flushBuffer();
    }

    /**
     * 将某个对象转换成json格式并发送到客户端
     * @param response
     * @param obj
     * @throws Exception
     */
    public void sendJsonMessage(HttpServletResponse response, Object obj) throws Exception {
        response.setContentType("application/json; charset=utf-8");
        ServletOutputStream writer = response.getOutputStream();
//        PrintWriter writer = response.getWriter();
        writer.write(JsonUtils.toJSon(obj).getBytes("utf-8"));
        writer.close();
        response.flushBuffer();
    }

}